package com.lonty.vote.service;

import com.lonty.vote.mapper.master.UserMapper;
import com.lonty.vote.mapper.master.VoteRecordsMapper;
import com.lonty.vote.mapper.second.BackupsUserMapper;
import com.lonty.vote.mapper.second.BackupsVoteRecordsMapper;
import com.lonty.vote.model.User;
import com.lonty.vote.model.VoteRecords;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 陈璇
 * @Date 2020/6/4 14:28
 * @Version 1.0
 */
@Service
public class BackupsService {

    @Resource
    private BackupsUserMapper backupsUserMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private VoteRecordsMapper voteRecordsMapper;

    @Resource
    private BackupsVoteRecordsMapper backupsVoteRecordsMapper;

    /**
     * 备份用户
     */
    @Transactional
    public void backupsUser(){
        //获取未备份用户列表和ID
        List<User> userList = userMapper.getBackupsUser();
        List<String> ids = userMapper.getBackupsUserIds();
        if(userList.size() > 0 && ids.size() > 0){
            //备份数据到从库
            backupsUserMapper.backupsUser(userList);
            //修改主库用户备份状态
            userMapper.modifyUserBackups(ids);
        }
    }

    /**
     * 备份投票记录
     */
    @Transactional
    public void backupsVoteRecords(){
        //获取未备份投票记录列表和ID
        List<VoteRecords> voteRecordsList = voteRecordsMapper.getBackupsVoteRecords();
        List<String> ids = voteRecordsMapper.getBackupsVoteRecordsIds();
        if(voteRecordsList.size() > 0 && ids.size() > 0){
            //备份数据到从库
            backupsVoteRecordsMapper.backupsVoteRecords(voteRecordsList);
            //修改主库用户备份状态
            voteRecordsMapper.modifyVoteRecordsBackups(ids);
        }
    }

}
